/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assigment.Almacenamiento;
import Miscelanea.Almacenamiento.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Gunner
 */
public class Ejercicio2 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        //Lista de Variables a utilizar
        int aux=1;
        List<Atributo> attrs = new ArrayList<Atributo>();
        ArrayList<Tupla> ListaTuplas=new ArrayList<Tupla>();
       
        
        do{
        
        Scanner Lector=new Scanner(System.in);
        System.out.println("                MENU");
        System.out.println("             ==========");
        System.out.println("1.- INGRESAR ESQUEMA DE RELACION");
        System.out.println("2.- INGRESAR  TUPLAS");
        System.out.println("3.- MOSTRAR TUPLAS");
        System.out.println("4.- EXIT");
        System.out.println(" INGRESE OPCION  : ");
         int Opcion = Lector.nextInt();
         switch (Opcion) {
            case 1:
                
                
                
                
                System.out.println("            ESQUEMA DE UNA RELACION");
                System.out.println("            =======================");
                System.out.print("Numero de atributos de la Relacion : ");
                int NumeroAtri=Lector.nextInt(); 
                
                
                for (int i = 1; i < NumeroAtri+1; i++) {               
                System.out.println("Atributo Numero "+i);               
                System.out.println("-------------------");
                System.out.println("Tipo atributo (Character, Byte,Short,Integer, Long, Float,Double, String): ");
                System.out.println("Tipo: ");
                String Tipo=Lector.next();              
                System.out.print("Nombre Atributo : ");
                String Nombre=Lector.next();
                //------------------------------------------------------
                //------------------------------------------------------                                
                
                    switch (Tipo) {
                        case "Character":
                            attrs.add(new Atributo(Nombre, Character.class));
                            break;
                        case "character":
                            attrs.add(new Atributo(Nombre, Character.class));
                            break;
                        case "Byte":
                            attrs.add(new Atributo(Nombre, Byte.class));;
                            break;
                        case "byte":
                            attrs.add(new Atributo(Nombre, Byte.class));
                            break;
                        case "Short":
                            attrs.add(new Atributo(Nombre, Short.class));
                            break;
                        case "short":
                            attrs.add(new Atributo(Nombre, Short.class));
                            break;
                        case "Integer":
                            attrs.add(new Atributo(Nombre, Integer.class));
                            break;
                        case "integer":
                            attrs.add(new Atributo(Nombre, Integer.class));
                            break;
                        case "Long":
                            attrs.add(new Atributo(Nombre, Long.class));
                            break;
                        case "long":
                            attrs.add(new Atributo(Nombre, Long.class));
                            break;
                        case "Float":
                            attrs.add(new Atributo(Nombre, Float.class));
                            break;
                        case "float":
                            attrs.add(new Atributo(Nombre, Float.class));
                            break;
                        case "Double":
                            attrs.add(new Atributo(Nombre, Double.class));
                            break;
                        case "double":
                            attrs.add(new Atributo(Nombre, Double.class));
                            break;
                        case "String":
                            attrs.add(new Atributo(Nombre, String.class));
                            break;
                        case "string":
                            attrs.add(new Atributo(Nombre, String.class));
                            break;
                            
                        default:
                            
                            throw new AssertionError();
                    }
                
                    System.out.println("-----------------------------------------------------");
                }
                break;
            //-------------------------------------------------------------------------------------------------------
            //-------------------------------------------------------------------------------------------------------
            //-------------------------------------------------------------------------------------------------------
            //---------- INGRESO DE TUPLAS --------------
            case 2:
                List<Comparable> v = new ArrayList<Comparable>();
                ArrayList<Tupla> Tuplas=new ArrayList<Tupla>();
                
                System.out.println("            INGRESO DE TUPLAS");
                System.out.println("            ==================");
                System.out.print("Numero de tuplas a ingresar: ");
                int NumeroTup=Lector.nextInt();
                
                
                
                
                for (int i = 0; i < NumeroTup; i++) {
                    v.clear();
                    System.out.println("TUPLA NUMERO "+(i+1));
                    for (int j = 0; j < attrs.size(); j++) {
                        String XXX=attrs.get(j).getTipo().getSimpleName(); 
                        Character A1;
                        byte A2;
                        Short A3;
                        Integer A4;
                        Long A5;
                        Float A6;
                        Double A7;
                        String A8;
                        
                        switch (XXX) {
                        case "Character":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A1=Lector.next().charAt(0);
                            v.add(new Character(A1));
                            
                            break;
                       
                        case "Byte":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A2=Lector.nextByte();
                            v.add(new Byte((byte) A2));
                            break;
                        case "byte":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A2=Lector.nextByte();
                            v.add(new Byte((byte) A2));
                            break;
                        case "Short":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A3=Lector.nextShort();
                            v.add(new Short((short) A3));
                            break;
                        
                        case "Integer":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A4=Lector.nextInt();
                            v.add(new Integer(A4));
                            break;
                      
                        case "Long":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A5=Lector.nextLong();
                            v.add(new Long(A5));
                            break;
                        
                        case "Float":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A6=Lector.nextFloat();
                            v.add(new Float(A6));
                            break;
                        
                        case "Double":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A7=Lector.nextDouble();
                            v.add(new Float(A7));
                            break;
                        
                        case "String":
                            System.out.print(attrs.get(j).getNombre()+ " ("+ XXX + ") : ");
                            A8=Lector.next();
                            v.add(new String(A8));
                            break;
                       
                            
                        default:
                            
                            throw new AssertionError();
                    }
                      
                    }
                   Tupla t = new Tupla(new IdentificadorTupla("redes", 0), v);
                   Tuplas.add(t); 
                   
                }
                ListaTuplas=Tuplas;
                
               
                
                
                  
                break;
            //-------------------------------------------------------------------------------------------------------
            //-------------------------------------------------------------------------------------------------------
            //-------------------------------------------------------------------------------------------------------
            case 3:
                
                
                java.io.RandomAccessFile raf = new java.io.RandomAccessFile("redes", "rw");
                
                Relacion MiRelacion=new Relacion(attrs);
                //LISTAMOS LAS TUPLAS INGRESADAS
                System.out.println("           LISTA DE TUPLAS");
                System.out.println("           ===============");
                
               for( int i=0;i<ListaTuplas.size();i++)
            {
                GestorIOTupla man = new GestorIOTupla(MiRelacion, ListaTuplas.get(i).getIdTupla().getNombreArchivo());
                
                System.out.println("writing tuple..." + ListaTuplas.get(i));
                byte[] bytes = new byte[1024];
                //man.escribirTupla(t, raf);
                man.escribirTupla(ListaTuplas.get(i), bytes, 0);
                raf.close();
                
                
                raf = new java.io.RandomAccessFile("redes", "r");
                System.out.println("reading tuple...");
                // t = man.leerTupla(bytes);
                Par<Tupla, Integer> st = man.leerTupla(bytes, 0);
                System.out.println(st);
                raf.close();
                
            }
                break;
            default:
                throw new AssertionError();
        }
        }
        while(aux==1);
        
    }
}
